package com.nami.framework.security.handler;

import com.nami.common.response.R;
import com.nami.common.response.ResponseCodeEnum;
import com.nami.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义用户被剔除
 * 用户异地登录,前一次被剔除
 *
 * @author C
 */
@Component
@Slf4j
public class SecuritySessionExpiredHandler implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
        log.info("剔除用户操作实现接口");
        HttpServletRequest request = event.getRequest();
        HttpServletResponse response = event.getResponse();
        if (ServletUtils.isAjax(request)) {
            R<Void> r = R.failed(ResponseCodeEnum.FAILED_406);
            ServletUtils.writeJson(r);
        } else {
            response.sendRedirect("/login");
        }
    }
}
